/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chessboard;

/**
 *
 * @author Jedrek
 */
public class CheckChecker {
    
    private static char figure;
    
    /**
     * Checks if given field is checked (forbiten for king)
     * @param boardState
     * @param x coordinate on letters (A is 0)
     * @param y coordinate on numbers (1 is 0)
     * @param figure letter describing type of figure for which we are checking (important is if it's uppercase or lowercase,
     * we have to determin who is our enemy
     * @return true if the field is checked (forbitten for king
     */
    public static boolean isChecked(char[][] boardState, int x, int y, char figure){
        CheckChecker.figure = figure;
        int[][] knP = {{x+1, y+2},
                       {x+1, y-2},
                       {x-1, y+2},
                       {x-1, y-2},
                       {x+2, y+1},
                       {x+2, y-1},
                       {x-2, y+1},
                       {x-2, y-1}};
        
        //pawns
        if(Character.isUpperCase(figure)){
            try{
                if(boardState[x+1][y+1] == 'p' || boardState[x-1][y+1] == 'p')
                return true;
            } catch (Exception e){};
        } else {
            try{
                if(boardState[x+1][y-1] == 'P' || boardState[x-1][y-1] == 'P')
                return true;
            } catch (Exception e){};
        }
        
        //knights
        for(int i = 0; i<8; i++){
            try{
                if(isOpposite(boardState[knP[i][0]][knP[i][1]]) && 
                    (boardState[knP[i][0]][knP[i][1]]=='n' ||
                    boardState[knP[i][0]][knP[i][1]]=='N'))
                return true;
            } catch(Exception e){}
        }
        
        char b = 'b';
        char r = 'r';
        char q = 'q';
        char k = 'k';
        if(Character.isLowerCase(figure)){
            b = 'B';
            r = 'R';
            q = 'Q';
            k = 'K';
        }
        
        //King
        for(int i = -1; i<2; i++)
            for(int j = -1; j<2; j++){
                if(i==0 && j==0)
                    continue;
                try{
                    if(boardState[x+i][y+j] == k)
                        return true;
                } catch(Exception e){}
            }
        
        //queen is checked with rook and bishop
        //rook
        //into lower letters
        for(int i = x-1; i>=0; i--){
            if(boardState[i][y] == r || boardState[i][y] == q)
                return true;
            if(boardState[i][y] != '0') //czy cos blokuje
                break;
        }
        //into higher letters
        for(int i = x+1; i<8; i++){
            if(boardState[i][y] == r || boardState[i][y] == q)
                return true;
            if(boardState[i][y] != '0') //czy cos blokuje
                break;
        }
        //into lower numbers
        for(int i = y-1; i>=0; i--){
            if(boardState[x][i] == r || boardState[x][i] == q)
                return true;
            if(boardState[x][i] != '0') //czy cos blokuje
                break;
        }
        //into higher numbers
        for(int i = y+1; i<8; i++){
            if(boardState[x][i] == r || boardState[x][i] == q)
                return true;
            if(boardState[x][i] != '0') //czy cos blokuje
                break;
        }
        
        //bishop
        //into lower letters and numbers
        int n, c; //c-characters, n-numbers
        for(n=y-1, c=x-1; n>=0 && c>=0; n--, c--){
            if(boardState[c][n] == b || boardState[c][n] == q)
                return true;
            if(boardState[c][n] != '0')
                break;
        }
        //into lower letters and higher numbers
        for(n=y+1, c=x-1; n<8 && c>=0; n++, c--){
            if(boardState[c][n] == b || boardState[c][n] == q)
                return true;
            if(boardState[c][n] != '0')
                break;
        }
        //into higher letters and lower numbers
        for(n=y-1, c=x+1; n>=0 && c<8; n--, c++){
            if(boardState[c][n] == b || boardState[c][n] == q)
                return true;
            if(boardState[c][n] != '0')
                break;
        }
        //into higher letters and numbers
        for(n=y+1, c=x+1; n<8 && c<8; n++, c++){
            if(boardState[c][n] == b || boardState[c][n] == q)
                return true;
            if(boardState[c][n] != '0')
                break;
        }
        
        return false;
    }
    
    private static boolean isOpposite(char c){
        if(Character.isLowerCase(figure) && Character.isUpperCase(c))
            return true;
        if(Character.isLowerCase(c) && Character.isUpperCase(figure))
            return true;
        
        return false;
    }
}
